import { _decorator, Component, easing, Label, Node, Sprite, tween, Vec3 } from 'cc';
import { ResMgr } from '../Mgr/ResMgr';
import { Global } from '../Global';
import { GAMEDATA } from './GAMEDATA';
import { GameScene } from './GameScene';
const { ccclass, property } = _decorator;

@ccclass('SingleCat')
/**
 * 单只猫咪
 */
export class SingleCat extends Component {

    //猫咪纹理
    @property(Sprite)
    private CatSprite: Sprite = null;

    //对话框
    @property(Node)
    private Dialog: Node = null;

    //水果
    @property(Sprite)
    private Fruit: Sprite = null;

    //水果数量
    @property(Label)
    private FruitLabel: Label = null;

    //水果数量
    public FruitNum: number = 0;
    public FixNum: number = 0;

    //水果索引
    public FruitIndex: number = 0;


    /**
     * 更新水果数量
     */
    public UpdateFruitNum(_num: number, _fruit: number) {
        this.FruitNum = _num;
        this.FruitIndex = _fruit;
        this.FixNum = this.FruitNum;

        let cat_index: number = GAMEDATA.CAT_STYLE[Math.floor(GAMEDATA.CAT_STYLE.length * Math.random())]
        this.CatSprite.spriteFrame = ResMgr.Instance.SpriteFrames["game_cat" + cat_index];
        this.FruitLabel.string = this.FruitNum.toString();
        this.Fruit.spriteFrame = ResMgr.Instance.SpriteFrames["game_fruit" + this.FruitIndex];

        this.Dialog.setScale(0, 0);
        this.CatSprite.node.setPosition(new Vec3(0, -100));

        tween(this.CatSprite.node).to(0.1, { position: new Vec3(0, 0) }, { easing: easing.backOut }).call(() => {
            tween(this.Dialog).to(0.1, { scale: new Vec3(0.7, 0.7) }).start();
        }).start();
    }

    /**
     * 补齐
     */
    public CompleteFruit(_num: number) {
        this.FruitNum -= _num;
        if (this.FruitNum <= 0) {
            this.FruitNum = 0;
            GAMEDATA.GameCoin += Math.floor(this.FixNum / 3) * (GAMEDATA.IsDouble ? 2 : 1);
            GameScene.Instance.UpdateCoin();
        }
        this.FruitLabel.string = this.FruitNum.toString();

        tween(this.Fruit.node).to(0.1, { scale: new Vec3(0.8, 0.8) }).to(0.1, { scale: new Vec3(1, 1) }).start();
    }


}


